/*
 * @Author: your name
 * @Date: 2022-02-25 22:06:57
 * @LastEditTime: 2022-03-23 19:39:10
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \test\websocket_main.cpp
 */
#include "websocket_client.h"
#include <iostream>
#include <Windows.h>

using namespace std;
int main()
{
    WORD wdVersion = MAKEWORD(2, 2);              //定义自己需要的网络库版本，这里是2.2
    WSADATA wdSockMsg;                            //这是一个结构体
    int nRes = WSAStartup(wdVersion, &wdSockMsg); //打开一个套接字
    if (0 != nRes)
    {
        switch (nRes)
        {
            case WSASYSNOTREADY:
                printf("check your library");
                break;
            case WSAVERNOTSUPPORTED:
                printf("need updated");
                break;
            case WSAEINPROGRESS:
                printf("need reboot");
                break;
            case WSAEPROCLIM:
                printf("sdfsdfsa");
                break;
        }
    }
    if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion))
    {
        printf("WSACleanup");
        WSACleanup();
        return 0;
    }
    
    WebsocketClient *p1 = new WebsocketClient("wss://117.50.176.114:8080/echo",true);
    p1->SetOnConnectedHandler([](WebsocketClient *wscli){
        if(nullptr != wscli){
            std::cout<<"on connected from " <<wscli->State()<<std::endl;
        }
    });
    p1->SetOnDisConnectedHandler([](WebsocketClient *p,WebsocketClient::CloseReason reason){
        if(nullptr != p){
            std::cout<<"DisConnectedHandler reason"<<reason<<std::endl;
        }
    });
    p1->SetOnReadHandler([](WebsocketClient *p, std::string str){
        if(nullptr != p){
            std::cout<<"OnReadHandler"<<str<<std::endl;
        }
    });
    int count = 0;
    while (1)
    {
        // std::cout<<"124"<<std::endl;
        Sleep(4000);
        if(p1 != nullptr)
            p1->SendMsg("12345",1,websocketpp::frame::opcode::TEXT);
        if(count == 5){
            std::cout<<"1234567";
            delete p1;
            p1 = nullptr;
        }
        count++;
    }
}
